package test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.File;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileSystemView;

public class FileDropFrame extends JFrame {
    private JPanel filePanel;

    public FileDropFrame() {
        initializeUI();
    }

    private void initializeUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setTitle("文件拖放示例");

        // 主面板使用滚动窗口
        filePanel = new JPanel();
        filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.Y_AXIS));
        JScrollPane scrollPane = new JScrollPane(filePanel);
        add(scrollPane, BorderLayout.CENTER);

        // 设置拖放目标
        new DropTarget(getContentPane(), new FileDropHandler());
    }

    private class FileDropHandler implements DropTargetListener {
        @Override
        public void dragEnter(DropTargetDragEvent dtde) {
            handleDragEvent(dtde);
        }

        @Override
        public void dragOver(DropTargetDragEvent dtde) {}

        @Override
        public void dropActionChanged(DropTargetDragEvent dtde) {}

        @Override
        public void dragExit(DropTargetEvent dte) {}

        @Override
        public void drop(DropTargetDropEvent dtde) {
            try {
                dtde.acceptDrop(DnDConstants.ACTION_COPY);
                Transferable transferable = dtde.getTransferable();
                
                if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                    List<File> files = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
                    updateFileList(files);
                    dtde.dropComplete(true);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                dtde.dropComplete(false);
            }
        }

        private void handleDragEvent(DropTargetDragEvent dtde) {
            if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                dtde.acceptDrag(DnDConstants.ACTION_COPY);
            } else {
                dtde.rejectDrag();
            }
        }
    }

    private void updateFileList(List<File> files) {
        SwingUtilities.invokeLater(() -> {
            filePanel.removeAll();
            FileSystemView fsv = FileSystemView.getFileSystemView();
            
            for (File file : files) {
                // 创建文件条目面板
                JPanel entry = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 5));
                entry.setMaximumSize(new Dimension(400, 40));
                
                // 获取系统图标和文件名
                Icon icon = fsv.getSystemIcon(file);
                JLabel iconLabel = new JLabel(icon);
                JLabel nameLabel = new JLabel(file.getName());
                
                // 添加组件
                entry.add(iconLabel);
                entry.add(nameLabel);
                filePanel.add(entry);
            }
            
            filePanel.revalidate();
            filePanel.repaint();
        });
    }

    public static void main(String[] args) {
    	
    	// 添加这两行系统属性设置（必须在Swing初始化前）
        System.setProperty("sun.awt.dnd.disabledDnd", "false");
        System.setProperty("sun.java2d.noddraw", "true");
        SwingUtilities.invokeLater(() -> {
            FileDropFrame frame = new FileDropFrame();
            frame.setVisible(true);
        });
    }
}